// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl;

internal sealed partial class GeoDistanceQueryConverter : JsonConverter<GeoDistanceQuery>
{
	public override GeoDistanceQuery Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		var variant = new GeoDistanceQuery();
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				var property = reader.GetString();
				if (property == "_name")
				{
					variant.QueryName = JsonSerializer.Deserialize<string?>(ref reader, options);
					continue;
				}

				if (property == "boost")
				{
					variant.Boost = JsonSerializer.Deserialize<float?>(ref reader, options);
					continue;
				}

				if (property == "distance")
				{
					variant.Distance = JsonSerializer.Deserialize<string>(ref reader, options);
					continue;
				}

				if (property == "distance_type")
				{
					variant.DistanceType = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType?>(ref reader, options);
					continue;
				}

				if (property == "validation_method")
				{
					variant.ValidationMethod = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod?>(ref reader, options);
					continue;
				}

				variant.Field = property;
				reader.Read();
				variant.Location = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.GeoLocation>(ref reader, options);
			}
		}

		return variant;
	}

	public override void Write(Utf8JsonWriter writer, GeoDistanceQuery value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.Field is not null && value.Location is not null)
		{
			if (!options.TryGetClientSettings(out var settings))
			{
				ThrowHelper.ThrowJsonExceptionForMissingSettings();
			}

			var propertyName = settings.Inferrer.Field(value.Field);
			writer.WritePropertyName(propertyName);
			JsonSerializer.Serialize(writer, value.Location, options);
		}

		if (!string.IsNullOrEmpty(value.QueryName))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(value.QueryName);
		}

		if (value.Boost.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(value.Boost.Value);
		}

		writer.WritePropertyName("distance");
		JsonSerializer.Serialize(writer, value.Distance, options);
		if (value.DistanceType is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, value.DistanceType, options);
		}

		if (value.ValidationMethod is not null)
		{
			writer.WritePropertyName("validation_method");
			JsonSerializer.Serialize(writer, value.ValidationMethod, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(GeoDistanceQueryConverter))]
public sealed partial class GeoDistanceQuery : SearchQuery
{
	public string? QueryName { get; set; }
	public float? Boost { get; set; }

	/// <summary>
	/// <para>The radius of the circle centred on the specified location.<br/>Points which fall into this circle are considered to be matches.</para>
	/// </summary>
	public string Distance { get; set; }

	/// <summary>
	/// <para>How to compute the distance.<br/>Set to `plane` for a faster calculation that's inaccurate on long distances and close to the poles.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceType { get; set; }
	public Elastic.Clients.Elasticsearch.Serverless.Field Field { get; set; }
	public Elastic.Clients.Elasticsearch.Serverless.GeoLocation Location { get; set; }

	/// <summary>
	/// <para>Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or longitude.<br/>Set to `COERCE` to also try to infer correct latitude or longitude.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod? ValidationMethod { get; set; }

	public static implicit operator Query(GeoDistanceQuery geoDistanceQuery) => QueryDsl.Query.GeoDistance(geoDistanceQuery);

	internal override void InternalWrapInContainer(Query container) => container.WrapVariant("geo_distance", this);
}

public sealed partial class GeoDistanceQueryDescriptor<TDocument> : SerializableDescriptor<GeoDistanceQueryDescriptor<TDocument>>
{
	internal GeoDistanceQueryDescriptor(Action<GeoDistanceQueryDescriptor<TDocument>> configure) => configure.Invoke(this);

	public GeoDistanceQueryDescriptor() : base()
	{
	}

	private string? QueryNameValue { get; set; }
	private float? BoostValue { get; set; }
	private string DistanceValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceTypeValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod? ValidationMethodValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field FieldValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoLocation LocationValue { get; set; }

	public GeoDistanceQueryDescriptor<TDocument> QueryName(string? queryName)
	{
		QueryNameValue = queryName;
		return Self;
	}

	public GeoDistanceQueryDescriptor<TDocument> Boost(float? boost)
	{
		BoostValue = boost;
		return Self;
	}

	/// <summary>
	/// <para>The radius of the circle centred on the specified location.<br/>Points which fall into this circle are considered to be matches.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor<TDocument> Distance(string distance)
	{
		DistanceValue = distance;
		return Self;
	}

	/// <summary>
	/// <para>How to compute the distance.<br/>Set to `plane` for a faster calculation that's inaccurate on long distances and close to the poles.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor<TDocument> DistanceType(Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? distanceType)
	{
		DistanceTypeValue = distanceType;
		return Self;
	}

	/// <summary>
	/// <para>Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or longitude.<br/>Set to `COERCE` to also try to infer correct latitude or longitude.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor<TDocument> ValidationMethod(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod? validationMethod)
	{
		ValidationMethodValue = validationMethod;
		return Self;
	}

	public GeoDistanceQueryDescriptor<TDocument> Location(Elastic.Clients.Elasticsearch.Serverless.GeoLocation location)
	{
		LocationValue = location;
		return Self;
	}

	public GeoDistanceQueryDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Serverless.Field field)
	{
		FieldValue = field;
		return Self;
	}

	public GeoDistanceQueryDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (FieldValue is not null && LocationValue is not null)
		{
			var propertyName = settings.Inferrer.Field(FieldValue);
			writer.WritePropertyName(propertyName);
			JsonSerializer.Serialize(writer, LocationValue, options);
		}

		if (!string.IsNullOrEmpty(QueryNameValue))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(QueryNameValue);
		}

		if (BoostValue.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(BoostValue.Value);
		}

		writer.WritePropertyName("distance");
		JsonSerializer.Serialize(writer, DistanceValue, options);
		if (DistanceTypeValue is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, DistanceTypeValue, options);
		}

		if (ValidationMethodValue is not null)
		{
			writer.WritePropertyName("validation_method");
			JsonSerializer.Serialize(writer, ValidationMethodValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class GeoDistanceQueryDescriptor : SerializableDescriptor<GeoDistanceQueryDescriptor>
{
	internal GeoDistanceQueryDescriptor(Action<GeoDistanceQueryDescriptor> configure) => configure.Invoke(this);

	public GeoDistanceQueryDescriptor() : base()
	{
	}

	private string? QueryNameValue { get; set; }
	private float? BoostValue { get; set; }
	private string DistanceValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceTypeValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod? ValidationMethodValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field FieldValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoLocation LocationValue { get; set; }

	public GeoDistanceQueryDescriptor QueryName(string? queryName)
	{
		QueryNameValue = queryName;
		return Self;
	}

	public GeoDistanceQueryDescriptor Boost(float? boost)
	{
		BoostValue = boost;
		return Self;
	}

	/// <summary>
	/// <para>The radius of the circle centred on the specified location.<br/>Points which fall into this circle are considered to be matches.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor Distance(string distance)
	{
		DistanceValue = distance;
		return Self;
	}

	/// <summary>
	/// <para>How to compute the distance.<br/>Set to `plane` for a faster calculation that's inaccurate on long distances and close to the poles.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor DistanceType(Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? distanceType)
	{
		DistanceTypeValue = distanceType;
		return Self;
	}

	/// <summary>
	/// <para>Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or longitude.<br/>Set to `COERCE` to also try to infer correct latitude or longitude.</para>
	/// </summary>
	public GeoDistanceQueryDescriptor ValidationMethod(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoValidationMethod? validationMethod)
	{
		ValidationMethodValue = validationMethod;
		return Self;
	}

	public GeoDistanceQueryDescriptor Location(Elastic.Clients.Elasticsearch.Serverless.GeoLocation location)
	{
		LocationValue = location;
		return Self;
	}

	public GeoDistanceQueryDescriptor Field(Elastic.Clients.Elasticsearch.Serverless.Field field)
	{
		FieldValue = field;
		return Self;
	}

	public GeoDistanceQueryDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public GeoDistanceQueryDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (FieldValue is not null && LocationValue is not null)
		{
			var propertyName = settings.Inferrer.Field(FieldValue);
			writer.WritePropertyName(propertyName);
			JsonSerializer.Serialize(writer, LocationValue, options);
		}

		if (!string.IsNullOrEmpty(QueryNameValue))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(QueryNameValue);
		}

		if (BoostValue.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(BoostValue.Value);
		}

		writer.WritePropertyName("distance");
		JsonSerializer.Serialize(writer, DistanceValue, options);
		if (DistanceTypeValue is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, DistanceTypeValue, options);
		}

		if (ValidationMethodValue is not null)
		{
			writer.WritePropertyName("validation_method");
			JsonSerializer.Serialize(writer, ValidationMethodValue, options);
		}

		writer.WriteEndObject();
	}
}